<?php 
/**
 * Template Name: Doctors Listing
 * @package WordPress
 * @subpackage WelCare
 */

get_header();?>

<div class="container">

	<!-- Page banner -->
	<?php fw_subpages_banner('doctor_listing'); ?>
	
	<!-- Breadcrumbs -->
	<div class="breadcrumbs">
		<?php echo get_the_breadcrumb(); /** add breadcrumbs */?>
	</div>
	
	   <?php $sidebar = get_post_meta($queried_obj->ID, 'wl_custom_sidebar', true); 
		//echo $sidebar ; exit ; 
	$contents_area = ( $sidebar != -1) ? 'contents_area' : '';
	$contents = ( $sidebar != -1) ? 'contents' : '';
	?>

	<!--Content Area -->
	<div id="<?php echo $contents_area; ?>">
		<div class="<?php echo $contents; ?>">
			<?php
				$page = isset($_GET['paged']) ? $_GET['paged'] : 1;
				$number = get_option('posts_per_page');
				$offset = ($page - 1) * $number;
				
				$args = array('role'=>'doctors', 'number'=>$number, 'offset'=>$offset);
				
				/** Create a doctor search form query */
				$meta_query = array();

				if($_webnukes->kvalue($_GET, 'spacialities') && $_GET['spacialities'] != __('Select Value', THEME_NAME)) $meta_query[] = array('key'=>'primarysp', 'value'=>$_GET['spacialities'], 'compare'=>'LIKE');
				if($_webnukes->kvalue($_GET, 'city')) $meta_query[] = array('key'=>'city', 'value'=> $_GET['city'], 'compare'=>'=');
				if($_webnukes->kvalue($_GET, 'state')) $meta_query[] = array('key'=>'state', 'value'=> $_GET['state'], 'compare'=>'=');
				if($_webnukes->kvalue($_GET, 'rating')) $meta_query[] = array('key'=>'rating', 'value'=> $_GET['rating'], 'compare'=>'=');
				
				if($meta_query)
				{
					$args['meta_query']['relation'] = 'AND';
					$args['meta_query'] = $meta_query;
				}
				
				/** Query for doctors */
				$doctors = new WP_User_Query( $args );
				$doctors->query_where = $doctors->query_where." AND ".$wpdb->prefix."users.user_status = 1";
				$doctors->query();

				if($doctors->get_results())
				{
					foreach($doctors->get_results() as $count=>$doctor)
					{
						if( ! $doctor->user_status) continue;
						
						/** get the doctors meta and store it into the same array */
						foreach((array)get_user_meta($doctor->ID) as $k=>$v) $doctor->$k = $v[0];?>
						
						<div class="block <?php echo (count($doctors->get_results()) == ($count + 1)) ? 'last' : ''; ?>">
							<h3 class="title"><?php echo $doctor->display_name;?></h3>
							<div class="snap"> <img src="<?php echo $doctor->profile_image; ?>" alt="" /> </div>
							
							<!--Profile-->
							<div class="profile">
								<?php if($doctor->phone): ?>
                                    <div class="phone">
                                        <?php echo $doctor->phone;?>
                                    </div>
                                <?php endif; ?>
								
								<?php if($doctor->major || $doctor->certifications): ?>
                                    <div class="certification">
                                        <?php if($doctor->certifications): ?>
                                        	<strong><?php _e('Certification', THEME_NAME);?>:</strong> <?php echo $doctor->certifications;?>
                                        <?php endif; ?>
                                        <?php if($doctor->major): ?>
                                        	<div class="links"><?php echo $doctor->major;?></div>
                                        <?php endif; ?>
                                    </div>
                                <?php endif; ?>
						
								<div class="ratings">
									<ul>
										<li><?php _e('Experience Level', THEME_NAME); ?></li>
										<li><?php _e('Industry Recognition', THEME_NAME); ?></li>
										<li><?php _e('Professional Conduct', THEME_NAME); ?></li>
									</ul>
									
									<ul class="rating-circles">
										<li data-type="doctor_experience" data-id="<?php echo $doctor->ID; ?>">
											<?php /** Start Circle rating*/ 
													$doctor->doctor_experience = isset($doctor->doctor_experience) ? $doctor->doctor_experience : serialize(array('ips'=>''));
													wl_doctor_rating( '1'.$doctor->ID, $doctor->doctor_experience);?>
											<br />
										</li>
										<li data-type="doctor_industry" data-id="<?php echo $doctor->ID; ?>">
											<?php /** Start Circle rating*/
													 $doctor->doctor_industry = isset($doctor->doctor_industry) ? $doctor->doctor_industry : serialize(array('ips'=>''));
													wl_doctor_rating( '2'.$doctor->ID, $doctor->doctor_industry);?>
											<br />
										</li>
										
										<li data-type="doctor_professional" data-id="<?php echo $doctor->ID; ?>">
											<?php /** Start Circle rating*/
													$doctor->doctor_professional = isset($doctor->doctor_professional) ? $doctor->doctor_professional : serialize(array('ips'=>''));
													wl_doctor_rating( '3'.$doctor->ID, $doctor->doctor_professional);?>
											<br />
										</li>
									</ul>
									
									<ul class="last-child">
										<li><?php _e('Average Patient Rating', THEME_NAME);?></li>
										<li>
											<div class="rating_heart" data-type="doctor_heart" data-id="<?php echo $doctor->ID; ?>">
												<?php /** Start Heart rating*/
														$doctor->doctor_heart = isset($doctor->doctor_heart) ? $doctor->doctor_heart : serialize(array('ips'=>''));
														wl_doctor_rating( '4'.$doctor->ID, $doctor->doctor_heart);?>
											</div>
										</li>
									</ul>
									
									<div class="clear"></div>
								</div>
							</div>
					
							<div class="details">
								<?php if($doctor->description): ?>
                                	<p>
                                        <strong class="blue"><?php _e('Quick Bio', THEME_NAME); ?></strong>
                                        <?php echo nl2br($doctor->description); ?> 
                                        <a href="#" class="read_more expand"><?php _e('read more', THEME_NAME);?></a>
                                    </p>
                                    <div class="biodata-text-more">
                                        <ul class="grid-3">
                                            <li>
                                                <h3><?php _e('Products &amp; Services', THEME_NAME); ?></h3>
                                                <ul class="style2">
                                                    <li><?php echo implode('</li><li>', explode(',', $doctor->primarysp)); /** Wrap the doctors services in li tag*/?></li>
                                                </ul>
                                            </li>
                                            
                                            <li>
                                                <h3><?php _e('Specialties', THEME_NAME);?></h3>
                                                <p><?php echo $doctor->specialities;?></p>
                                            </li>
                                            <li><img src="<?php echo $doctor->additional_image;?>" alt="" /></li>
                                        </ul>
                                        
                                        <div class="clear"></div>
                                        <?php echo $doctor->trainings;?>
                                    </div>
                                <?php endif; ?>
							</div>
						</div>
				<?php }?>
					<!--Pagination-->
					<?php the_pagination('total='. ceil($doctors->total_users / $number ));
				}
				else{?>
                	<div class="block">
                    	<p><?php _e('Sorry! no doctor matched with your current search criteria.', THEME_NAME); ?></p>
                    </div>
                <?php } ?>
		</div>
	</div>
	
    <?php if( $sidebar != -1): ?>
	<!--Sidebar-->
	<div id="side_bar">
		<?php if($sidebar = get_post_meta($post->ID, 'wl_custom_sidebar', true)) dynamic_sidebar($sidebar);
			else _load_dynamic_sidebar('doctor', 'blog-sidebar'); ?>
	</div>
    <?php endif; ?>

</div>

<?php get_footer();?>